package com.crrc.dataparse.util;

public class ByteConvertUtil {
    public ByteConvertUtil() {
    }

    public static String bytesToHexString(byte[] bytes) {
        int unitLength = 2;
        StringBuffer result = new StringBuffer();
        StringBuffer sb = new StringBuffer();
        byte[] var4 = bytes;
        int var5 = bytes.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            byte b = var4[var6];
            sb = sb.append(Integer.toHexString(b & 255));
            if (sb.length() < unitLength) {
                sb.insert(0, "0");
            }

            result.append(sb);
            sb.setLength(0);
        }

        return result.toString();
    }

    public static String bytesToBinaryString(byte[] bytes) {
        StringBuffer result = new StringBuffer();
        byte[] var2 = bytes;
        int var3 = bytes.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            byte b = var2[var4];
            result.append(byteToBitString(b));
        }

        return result.toString();
    }

    public static String byteToBitString(byte b) {
        return "" + (byte)(b >> 7 & 1) + (byte)(b >> 6 & 1) + (byte)(b >> 5 & 1) + (byte)(b >> 4 & 1) + (byte)(b >> 3 & 1) + (byte)(b >> 2 & 1) + (byte)(b >> 1 & 1) + (byte)(b >> 0 & 1);
    }

    public static String getByteBitString(byte b, int beginIndex, int endIndex) {
        String str = byteToBitString(b);
        String result = str.substring(beginIndex, endIndex);
        return result;
    }

    public static byte[] binaryStringToBytes(String str) {
        int size = str.length() / 8;
        int modo = str.length() % 8;
        if (modo == 1) {
            str = "0000000" + str;
        }

        if (modo == 2) {
            str = "000000" + str;
        }

        if (modo == 3) {
            str = "00000" + str;
        }

        if (modo == 4) {
            str = "0000" + str;
        }

        if (modo == 5) {
            str = "000" + str;
        }

        if (modo == 6) {
            str = "00" + str;
        }

        if (modo == 7) {
            str = "0" + str;
        }

        byte[] result;
        int index;
        String sub;
        if (modo == 0) {
            result = new byte[size];

            for(index = 0; index < str.length(); index += 8) {
                sub = str.substring(index, index + 8);
                result[index / 8] = (byte)Integer.parseInt(sub, 2);
            }
        } else {
            result = new byte[size + 1];

            for(index = 0; index < str.length(); index += 8) {
                sub = str.substring(index, index + 8);
                result[index / 8] = (byte)Integer.parseInt(sub, 2);
            }
        }

        return result;
    }

    public static String byteToASCString(byte[] byteArray, int begin, int end) {
        String result = "";

        for(int i = begin; i < end; ++i) {
            char temp = (char)byteArray[i];
            result = result + temp;
        }

        return result;
    }

    public static String binToHexString(String bin) {
        return Long.toString(Long.parseLong(bin, 2), 16).toUpperCase();
    }
}
